function PresentationManager() {
	
	this.loadingDiv = $('#loadingDiv');
	
	this.previewButton = $("#"+PresentationFieldIds.PREVIEW_BUTTON);
	this.emailButton = $("#"+PresentationFieldIds.EMAIL_BUTTON);
	
	this.saveSpan = $("#saveSpan");
	this.titleInput = $("#prezTitle");
	this.saveButton = $("#saveButton");
	this.idInput = $("#_id");
	
	PresentationManager.prototype.copyJSONObject = function(originalJSONObject) {
		return JSON.parse(JSON.stringify(originalJSONObject));
	}
	
	this.presentationDataEmpty = {};
	
	this.presentationDataDiff = this.copyJSONObject(this.presentationDataEmpty);
	this.presentationDataCurrent = this.copyJSONObject(this.presentationDataEmpty);
	
	this.slideManagers = new Array();
	
	for(var i=0; i<50; i++) {
		this.slideManagers[i] = new SlideManager(i, this);
	}
	
	PresentationManager.prototype.showPresentationPreview = function() {			
		var self = this;
		if(self.saveButton.is(':visible')) {
			$('#loadingDiv').fadeIn(250);
			this.savePresentationData(function(){
				var w = window.open("/presentationView.htm?_id="+self.idInput.val(),"","height=900,width=850, toolbar=no,directories=no,status=no,menubar=no, scrollbars=no,resizable=no,top=50,left=200");
		    }); 
		} else {
			var w = window.open("/presentationView.htm?_id="+self.idInput.val()+"&wait=1","","height=900,width=850, toolbar=no,directories=no,status=no,menubar=no, scrollbars=no,resizable=no,top=50,left=200");
		}
		
	};
	
	PresentationManager.prototype.emailPresentation = function() {			
		var self = this;
		if(self.saveButton.is(':visible')) {
			$('#loadingDiv').fadeIn(250);
			this.savePresentationData(function(){
				window.location.href = "mailto: ?Subject="+encodeURIComponent(self.titleInput.val())+"&Body="+encodeURIComponent("Click aici pentru a vedea pozele: http://prez73.herokuapp.com/presentationView.htm?v=4073498v7348nv7v87nb37byvn587y507yn35y70b7y59t0nt04v4740vbt973409tvb0&_id="+self.idInput.val());			
			}); 		
		} else {
			window.location.href = "mailto: ?Subject="+encodeURIComponent(self.titleInput.val())+"&Body="+encodeURIComponent("Click aici pentru a vedea pozele: http://prez73.herokuapp.com/presentationView.htm?v=4073498v7348nv7v87nb37byvn587y507yn35y70b7y59t0nt04v4740vbt973409tvb0&_id="+self.idInput.val());			
		}
	};
	
	var self = this;
	
	this.previewButton.click(function(){
		self.showPresentationPreview();		
    });
	
	this.emailButton.click(function(){
		self.emailPresentation();		
    });
	
	this.saveButton.click(function() {
		$('#loadingDiv').fadeIn(250);
		self.savePresentationData(); 
	});
	
	this.titleInput.on('change keyup paste', function() {
	    self.titleModifCallback(self.titleInput.val());
	});
	
	PresentationManager.prototype.titleModifCallback = function(newTitle) {
		if(newTitle != this.presentationDataCurrent["title"]) {
			this.presentationDataCurrent["title"] = newTitle;
			this.presentationDataDiff["title"] = newTitle;
			this.saveSpan.fadeIn(200);
		}
	};
	
	PresentationManager.prototype.imgModifCallback = function(slideIndex, newImg, newWidth, newHeight) {
		var realModif = false;
		var imgContentModif = false;
		if(newImg!=this.presentationDataCurrent["img"+slideIndex]) {
			this.presentationDataDiff["img"+slideIndex] = newImg;
			this.presentationDataCurrent["img"+slideIndex] = newImg;
			imgContentModif = true;
			realModif = true;
		}
		if(newWidth!=this.presentationDataCurrent["imgWidth"+slideIndex]) {
			this.presentationDataDiff["imgWidth"+slideIndex] = newWidth;
			this.presentationDataCurrent["imgWidth"+slideIndex] = newWidth;
			realModif = true;
		}
		if(newHeight!=this.presentationDataCurrent["imgHeight"+slideIndex]) {
			this.presentationDataDiff["imgHeight"+slideIndex] = newHeight;
			this.presentationDataCurrent["imgHeight"+slideIndex] = newHeight;
			realModif = true;
		}
		if(newImg == "REMOVED") {
			$("#textInput"+slideIndex).val("");
			this.presentationDataDiff["imgDesc"+slideIndex] = "";
		}
		if(imgContentModif) {
			$('#loadingDiv').fadeIn(250);
			this.savePresentationData();
		} else if(realModif) {
			this.saveSpan.fadeIn(200);
		}
	};
	
	PresentationManager.prototype.imgDescModifCallback = function(slideIndex, newImgDesc) {
		if(newImgDesc!=this.presentationDataCurrent["imgDesc"+slideIndex]) {
			this.presentationDataDiff["imgDesc"+slideIndex] = newImgDesc;
			this.presentationDataCurrent["imgDesc"+slideIndex] = newImgDesc;
			this.saveSpan.fadeIn(200);
		}
	};
	
	PresentationManager.prototype.savePresentationData = function(saveCompletedCallback) {
		$.ajax({
			 type:"POST",
		     url:'/presentation.htm',
		     data: self.presentationDataDiff,
		     success: function (data) {
		    	 self.idInput.val(data);
		    	 self.presentationDataCurrent["_id"] = data;
		    	 self.presentationDataEmpty["_id"] = data;
		    	 self.presentationDataDiff = self.copyJSONObject(self.presentationDataEmpty);
		    	 self.loadingDiv.hide();
		    	 self.saveSpan.fadeOut(200);
		    	 if(saveCompletedCallback) {
		    		 saveCompletedCallback();
		    	 }		    	 
		     },
		     timeout:1200000
		});
	};
	
	PresentationManager.prototype.copyJSONObject = function(originalJSONObject) {
		return JSON.parse(JSON.stringify(originalJSONObject));
	};
	
	PresentationManager.prototype.preload = function(presentationDataCurrent) {
		this.presentationDataCurrent = presentationDataCurrent;
		this.presentationDataDiff["_id"] = presentationDataCurrent["_id"];
		this.idInput.val(presentationDataCurrent["_id"]);
		this.titleInput.html(presentationDataCurrent["title"]);
		for(var i=0; i<50; i++) {
			if(presentationDataCurrent["img"+i] != undefined && presentationDataCurrent["img"+i] != "") {
				this.slideManagers[i].preloadSlide(presentationDataCurrent["img"+i], presentationDataCurrent["imgWidth"+i], presentationDataCurrent["imgHeight"+i], presentationDataCurrent["imgDesc"+i]);
			}
		}
	};
}